package com.wyh.service.serviceImpl.gfiveImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wyh.common.result.Result;
import com.wyh.domain.po.gfive.Journal;
import com.wyh.domain.po.gfive.PageHelper;
import com.wyh.mapper.gfivemapper.JournalMapper;
import com.wyh.service.gfive.JournalService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
* @author KmJHHhhhR
* @description 针对表【journal(日志表)】的数据库操作Service实现
* @createDate 2024-05-20 15:03:04
*/
@Service
public class JournalServiceImpl extends ServiceImpl<JournalMapper, Journal>
    implements JournalService{

    @Resource
    private JournalMapper journalMapper;

    @Override
    public Result<Map<String, Object>> searchJournals(String user, String function,
                                                      LocalDateTime startTime, LocalDateTime endTime, PageHelper pageHelper) {
        QueryWrapper<Journal> queryWrapper = new QueryWrapper<>();

        if (user != null && !user.isEmpty()) {
            queryWrapper.eq("user", user);
        }

        if (function != null && !function.isEmpty()) {
            queryWrapper.eq("`function`", function);
        }

        if (startTime != null && endTime != null) {
            queryWrapper.between("time", startTime, endTime);
        } else if (startTime != null) {
            return Result.error("400", "缺少参数：结束时间");
        } else if (endTime != null) {
            return Result.error("400", "缺少参数：开始时间");
        }

        Page<Journal> page = new Page<Journal>(pageHelper.getCurrentPage(), pageHelper.getPageSize());
        IPage<Journal> journalIPage = journalMapper.selectPage(page, queryWrapper);

        if (journalIPage.getRecords().size() == 0) {
            return Result.error();
        }

        Map<String, Object> resultData = new HashMap<>();
        resultData.put("records", journalIPage.getRecords());
        resultData.put("current", journalIPage.getCurrent());
        resultData.put("pages", journalIPage.getPages());
        resultData.put("total", journalIPage.getTotal());

        return Result.success(resultData);

    }


}




